package com.sshop.core.controller;

import cn.itcast.common.page.Pagination;
import com.sshop.core.bean.product.*;
import com.sshop.core.query.product.BrandQuery;
import com.sshop.core.query.product.FeatureQuery;
import com.sshop.core.query.product.ProductQuery;
import com.sshop.core.query.product.TypeQuery;
import com.sshop.core.service.product.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台商品列表展示页面
 * @author ljm
 *
 */
@Controller
public class FrontProductController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private ProductService productService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private FeatureService featureService;

    //商品列表页面
    @RequestMapping(value = "/product/display/list.shtml")
    public String list(Integer pageNo, Integer brandId, String brandName, Integer typeId, String typeName, String searchContent, ModelMap model) {
        //加载商品属性
        FeatureQuery featureQuery = new FeatureQuery();
        List<Feature> features = featureService.getFeatureList(featureQuery);
        //显示在页面
        model.addAttribute("features", features);
        //分页参数
        StringBuilder params = new StringBuilder();
        //设置页号
        ProductQuery productQuery = new ProductQuery();
        if (StringUtils.isNotBlank(searchContent)) {
            productQuery.setNameLike(true);
            productQuery.setName(searchContent);
            params.append("&").append("searchContent=").append(searchContent);
        }
        productQuery.setIsShow(1);
        productQuery.setPageNo(Pagination.cpn(pageNo));
        //设置每页数
        productQuery.setPageSize(Product.FRONT_PAGE_SIZE);
        //设置Id倒排
        productQuery.orderbyId(false);
        //隐藏已选条件
        boolean flag = false;
        //条件Map窗口
        Map<String, String> query = new LinkedHashMap<String, String>();
        //品牌ID
        if (null != brandId) {
            productQuery.setBrandId(brandId);
            flag = true;
            //显示在页面
            model.addAttribute("brandId", brandId);
            model.addAttribute("brandName", brandName);

            query.put("Brand", brandName);

            params.append("&").append("brandId=").append(brandId).append("&brandName=").append(brandName);
        } else {
            //加载商品品牌
            //品牌条件对象
            BrandQuery brandQuery = new BrandQuery();
            //设置指定字段
            brandQuery.setFields("id,name");
            //设置可见
            brandQuery.setIsDisplay(1);
            //加载品牌
            List<Brand> brands = brandService.getBrandList(brandQuery);
            //显示在页面
            model.addAttribute("brands", brands);
        }
        //类型ID
        if (null != typeId) {
            productQuery.setTypeId(typeId);
            flag = true;
            query.put("Type", typeName);
            //显示在页面
            model.addAttribute("typeId", typeId);
            model.addAttribute("typeName", typeName);
            params.append("&").append("typeId=").append(typeId).append("&typeName=").append(typeName);
        } else {
            //加载商品类型
            TypeQuery typeQuery = new TypeQuery();
            //指定查询哪些字段
            typeQuery.setFields("id,name");
            typeQuery.setIsDisplay(1);
            typeQuery.setParentId(1);
            List<Type> types = typeService.getTypeList(typeQuery);
            //显示在页面
            model.addAttribute("types", types);
        }
        model.addAttribute("flag", flag);
        //条件
        model.addAttribute("query", query);
        //加载带有分页的商品
        Pagination pagination = productService.getProductListWithPage(productQuery);
        List<Product> productList = (List<Product>) pagination.getList();
        for (int i = 0; i < productList.size(); i++) {
            Product product = productList.get(i);
            String name = product.getName();
            if (StringUtils.isNotBlank(searchContent)) {
                if (name.contains(searchContent)) {
                    name = name.replaceAll(searchContent, "<font color='red'>" + searchContent + "</font>");
                }
                product.setName(name);
            }
            Integer minPrice = productService.getMinPrice(product.getId());
            product.setMinPrice(minPrice);
        }
        model.addAttribute("searchContent", searchContent);
        String url = "/product/display/list.shtml";
        pagination.pageView(url, params.toString());
        model.addAttribute("pagination", pagination);
        return "product/product";
    }

    //跳转商品详情页
    @RequestMapping(value = "/product/detail.shtml")
    public String detail(Integer id, ModelMap model) {
        //商品加载
        Product product = productService.getProductByKey(id);
        model.addAttribute("product", product);
        //skus
        List<Sku> skus = skuService.getStock(id);
        model.addAttribute("skus", skus);
        //去重复
        List<Color> colors = new ArrayList<Color>();
        //遍历SKu
        for (Sku sku : skus) {
            //判断集合中是否已经有此颜色对象了
            if (!colors.contains(sku.getColor())) {
                colors.add(sku.getColor());
            }
        }
        model.addAttribute("colors", colors);

        return "product/productDetail";
    }

    @Autowired
    private SkuService skuService;
}
